package escritorio.Ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;

import escritorio.conn.AudienciaDao;
import escritorio.conn.ConnErro;
import escritorio.conn.ConnectionFactory;
import escritorio.javaBeans.Audiencia;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class AudienciaFrm extends JFrame {

	private JPanel contentPane;
	private JTextField txtDataAudiencia;
	private JTextArea txtParecer;
	private JComboBox cmbProcessos;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					AudienciaFrm frame = new AudienciaFrm();
					frame.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AudienciaFrm() {
		setTitle("Cadastro audi\u00EAncia");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 597, 481);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel label = new JLabel("Processo :");
		label.setBounds(12, 16, 76, 16);
		panel.add(label);

		cmbProcessos = new JComboBox<String>();
		cmbProcessos.setBounds(100, 13, 195, 22);
		fillcmbProcessos();
		panel.add(cmbProcessos);

		JLabel label_1 = new JLabel("Data :");
		label_1.setBounds(12, 56, 42, 16);
		panel.add(label_1);

		txtDataAudiencia = new JTextField();
		txtDataAudiencia.setToolTipText("Data no formato dd/mm/aaaa");
		txtDataAudiencia.setColumns(10);
		txtDataAudiencia.setBounds(100, 53, 116, 22);
		panel.add(txtDataAudiencia);

		JButton btnIncluir = new JButton("Incluir");
		btnIncluir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				adicionaAudiencia();
			}
		});
		btnIncluir.setBounds(307, 16, 97, 25);
		panel.add(btnIncluir);

		JButton btnEditar = new JButton("Editar");
		btnEditar.setBounds(307, 51, 97, 25);
		panel.add(btnEditar);

		JButton btnConsultar = new JButton("Consultar");
		btnConsultar.setBounds(307, 84, 97, 25);
		panel.add(btnConsultar);

		JButton btnSair = new JButton("Sair");
		btnSair.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnSair.setBounds(444, 51, 97, 25);
		panel.add(btnSair);

		JLabel lblParecer = new JLabel("Descri\u00E7\u00E3o :");
		lblParecer.setBounds(12, 88, 87, 16);
		panel.add(lblParecer);

		JPanel panelParecer = new JPanel();
		panelParecer.setBounds(40, 122, 517, 291);
		panel.add(panelParecer);
		panelParecer.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		panelParecer.add(scrollPane, BorderLayout.CENTER);

		txtParecer = new JTextArea();
		txtParecer.setFont(new Font("Monospaced", Font.PLAIN, 18));
		txtParecer.setLineWrap(true);
		scrollPane.setViewportView(txtParecer);

	}

	private void fillcmbProcessos() {
		String sql = "select nroProcesso from Processo";
		try {
			Connection connection = new ConnectionFactory().getConnection();
			PreparedStatement stmt = connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				String proc = rs.getString("nroProcesso");
				cmbProcessos.addItem(proc);
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			throw new ConnErro(null, e);
		}
	}

	protected void adicionaAudiencia() {
		Audiencia audi = new Audiencia();
		AudienciaDao daoA = new AudienciaDao();
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat data = new SimpleDateFormat("dd/MM/yyyy");
		try {
			cal.setTime(data.parse(txtDataAudiencia.getText()));
		} catch (ParseException e) {
			System.out.println("Erro no parse da data em AudienciaFrm");
			e.printStackTrace();
		}
		audi.setParecer(txtParecer.getText());
		audi.setDataAudiencia(cal);
		daoA.insereAudiencia(audi);

		System.out.println("Gravado!");
	}
}
